package com.hnkywl.sts.service.report;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.report.CwsfcDao;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.EasyuiAdapter;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-14
 * Time: 下午4:50
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CwsfcService {
    private CwsfcDao cwsfcDao;

    /**
     * 财务收发存报表
     *
     * @param gsid
     * @param year
     * @param month
     * @return
     */
    public String cwsfcList(Long gsid, Integer year, Integer month) {
        Map<String, List> result = cwsfcDao.cwsfcList(gsid, year, month);
        List<Map> rows = result.get("rows");
        List<Map> footer = result.get("footer");
        Gson gson = new GsonBuilder().create();
        String rowsJson = gson.toJson(rows);
        String footerJson = gson.toJson(footer);
        String json = "{\"rows\":" + rowsJson + ",\"footer\":" + footerJson + "}";
        return json;
    }

    /**
     * 销售成本结转
     *
     * @param year
     * @param month
     * @return
     */
    public String cbjz(Integer year, Integer month) {
        return cwsfcDao.cbjz(year, month);
    }


    /**
     * 财务收发存月末结转
     *
     * @param year
     * @param month
     * @return
     */
    public String sfcymjz(Integer year, Integer month) {
        return cwsfcDao.sfcymjz(year, month);
    }

    /**
     * 财务收发存明细
     *
     * @param codeId
     * @param gsId
     * @param startDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    public String cwsfcDetailList(Long gsId, String type, Integer codeId, Date startDate, Date endDate, int pageNo, int pageSize) {
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DATE, 1);
        endDate = c.getTime();
        Map<String, Object> map = cwsfcDao.cwsfcDetailList(gsId, type, codeId, startDate, endDate, pageNo, pageSize);
        Integer total = (Integer) map.get("total");
        List rows = (ArrayList) map.get("rows");
        List footer = (ArrayList) map.get("footer");
        return "{\"total\":" + total + ",\"rows\":" + new Gson().toJson(rows) + ",\"footer\":" + new Gson().toJson(footer) + "}";
    }

    /**
     * 财务收发存明细导出
     *
     * @param gsId
     * @param type
     * @param codeId
     * @param startDate
     * @param endDate
     * @return
     */
    public Map<String, Object> cwsfcDetailListExport(Long gsId, String type, Integer codeId, Date startDate, Date endDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DATE, 1);
        endDate = c.getTime();
        Map<String, Object> map = cwsfcDao.cwsfcDetailList(gsId, type, codeId, startDate, endDate, 0, 0);
        return map;
    }

    /**
     * 采购单统计明细
     *
     * @param gsId
     * @return
     */
    public String cgdtjmx(Integer codeId, Long gsId, Integer year, Integer month, int pageNo, int pageSize) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findCgdTjMx(codeId, gsId, dateStart, dateEnd, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(page.getResult()) +
                ",\"footer\":[{\"gsjc\":\"合计\",\"jsbhse\":\"" + map.get("sum") + "\"}]}";
    }

    public Map<String, Map> cgdtjmxExp(Integer codeId, Long gsId, Integer year, Integer month) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findCgdTjMx(codeId, gsId, dateStart, dateEnd, 1, -1);
        return map;
    }

    /**
     * 采购单估收统计明细
     *
     * @param gsId
     * @return
     */
    public String cgdGstjmx(Integer codeId, Long gsId, Date date, int pageNo, int pageSize) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            date = DateUtils.truncate(calendar.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findCgdGsTjMx(codeId, gsId, date, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(page.getResult()) +
                ",\"footer\":[{\"gsjc\":\"合计\",\"bhse\":\"" + map.get("sum") + "\"}]}";
    }

    //导出
    public Map<String, Map> cgdGstjmxExp(Integer codeId, Long gsId, Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            date = DateUtils.truncate(calendar.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findCgdGsTjMx(codeId, gsId, date, 1, -1);
        return map;
    }

    /**
     * 物流结算明细
     *
     * @param gsId
     * @return
     */
    public String wljsMx(Integer codeId, Long gsId, Integer year, Integer month, int pageNo, int pageSize) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findWljsMx(codeId, gsId, dateStart, dateEnd, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(page.getResult()) +
                ",\"footer\":[{\"gsjc\":\"合计\",\"jinge\":\"" + map.get("sum") + "\"}]}";
    }

    public Map<String, Map> wljsExp(Integer codeId, Long gsId, Integer year, Integer month) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findWljsMx(codeId, gsId, dateStart, dateEnd, 1, -1);
        return map;
    }

    /**
     * 物流估收明细
     *
     * @param gsId
     * @return
     */
    public String wlgsMx(Integer codeId, Long gsId, Date date, int pageNo, int pageSize) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            date = DateUtils.truncate(calendar.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findWlgsMx(codeId, gsId, date, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(page.getResult()) +
                ",\"footer\":[{\"gsjc\":\"合计\",\"je\":\"" + map.get("sum") + "\"}]}";
    }

    public Map<String, Map> wlGstjmxExp(Integer codeId, Long gsId, Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 1);
            date = DateUtils.truncate(calendar.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findWlgsMx(codeId, gsId, date, 1, -1);
        return map;
    }

    /**
     * 销售结算明细
     *
     * @param gsId
     * @return
     */
    public String xsjsMx(Integer codeId, Long gsId, Integer year, Integer month, int pageNo, int pageSize) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findXsjsMx(codeId, gsId, dateStart, dateEnd, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + new Gson().toJson(page.getResult()) +
                ",\"footer\":[{\"gs\":\"合计\",\"cnje\":\"" + ((Map) map.get("sum")).get("cnje") + "\",\"je\":\"" + ((Map) map.get("sum")).get("je") + "\"}]}";
    }

    public Map<String, Map> xsjsExp(Integer codeId, Long gsId, Integer year, Integer month) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findXsjsMx(codeId, gsId, dateStart, dateEnd, 1, -1);
        return map;
    }

    /**
     * 领料单明细
     *
     * @return
     */
    public String lldMx(Integer codeId, Long gsId, Integer year, Integer month, int pageNo, int pageSize) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd");
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Object> map = cwsfcDao.findLlMx(codeId, gsId, dateStart, dateEnd, pageNo, pageSize);
        Page page = (Page) map.get("page");
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + gsonBuilder.create().toJson(page.getResult()) +
                ",\"footer\":[{\"gsjc\":\"合计\",\"flje\":\"" + ((Map) map.get("sum")).get("je") + "\"}]}";
    }

    public Map<String, Map> lldExp(Integer codeId, Long gsId, Integer year, Integer month) {
        Date dateStart = null;
        Date dateEnd = null;
        if (year != null && month != null) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            dateStart = DateUtils.truncate(c.getTime(), 2);
            c.add(Calendar.MONTH, 1);
            dateEnd = DateUtils.truncate(c.getTime(), 2);
        }
        Map<String, Map> map = cwsfcDao.findLlMx(codeId, gsId, dateStart, dateEnd, 1, -1);
        return map;
    }

    @Autowired
    public void setCwsfcDao(CwsfcDao cwsfcDao) {
        this.cwsfcDao = cwsfcDao;
    }
}
